package handler

import (
	"MulticloudManagement/pkg/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	ecs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/region"
)

var cfg = utils.LoadConfig()

// HostList 获取云主机列表
func (a *Auth) HostList(c *gin.Context) {
	ak := cfg.Huawei.Ak
	sk := cfg.Huawei.Sk

	auth := basic.NewCredentialsBuilder().
		WithAk(ak).
		WithSk(sk).
		Build()

	client := ecs.NewEcsClient(
		ecs.EcsClientBuilder().
			WithRegion(region.ValueOf("cn-east-3")).
			WithCredential(auth).
			Build())

	request := &model.ListCloudServersRequest{}
	response, err := client.ListCloudServers(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
	//c.JSON(200, response.Servers)
	c.HTML(200, "hostlist.html", gin.H{
		"hosts": response.Servers,
	})
}
